const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        rooms: [],
        tabIndex: 0,
        pageIndex: 1, //页索引
        pageSize: 5, //页大小
        isShowLoad: false, //是否显示底部loading组件
        isNoMore: false //是否加载更多
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getLiveRoom();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        let self = this;
        self.setData({
            rooms: [],
            pageIndex: 1,
            isNoMore: false
        })
        self.getLiveRoom(); //重置
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let self = this;
        if (self.data.isNoMore) {
            return;
        }
        self.data.pageIndex++;
        self.getLiveRoom(); //加载更多
    },
    tabSelect: function (e) {
        this.setData({
            tabIndex: e.currentTarget.dataset.id,
            rooms: [],
            pageIndex: 1,
            isNoMore: false
        });
        this.getLiveRoom();
    },
    getLiveRoom: function () {
        let self = this;
        let url = app.globalData.apiRootUrl + "/Live/GetLiveList";

        let data = {
            PageSize: self.data.pageSize,
            PageIndex: self.data.pageIndex,
            playerUserId: parseInt(self.data.tabIndex) < 1 ? 0 : app.globalData.userInfo.UserID
        };

        self.setData({
            isShowLoad: true, //显示底部loading组件
        })

        app.httpRequest({
            url: url,
            data: data,
            method: "GET",
            success: function (res) {
                let rdata = res.data;
                if (rdata.status == 0) {
                    if (self.data.pageIndex == 1) {
                        self.setData({
                            rooms: rdata.data.rows
                        });
                    } else if (rdata.data.rows.length > 0) {
                        let rooms = self.data.rooms;
                        rooms = rooms.concat(rdata.data.rows);
                        self.setData({
                            rooms: rooms
                        });
                    }
                }
                //如果返回的数据长度小于页大小，则说明没有数据了
                if (rdata.data.rows.length < self.data.pageSize) {
                    self.setData({
                        isShowLoad: true,
                        isNoMore: true // true 没有数据了  false  还有数据
                    });
                } else {
                    self.setData({
                        isShowLoad: false //今日推荐是否显示底部loading组件
                    });
                }
            },
            fail: function (err) {
                console.log('获取第一页数据失败');
                self.setData({
                    isShowLoad: false //今日推荐是否显示底部loading组件
                });
            },
            complete: function () {
                wx.stopPullDownRefresh();
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    // 直播详情
    bindDetail: function (e) {
        var listString = '?';
        var self = this;
        var index = e.currentTarget.dataset.idx;
        var roomName = self.data.rooms[index].Description
        var anthor = self.data.rooms[index].Name
        var roomId = self.data.rooms[index].RoomID;
        var avatar = self.data.rooms[index].WeChartUserHead;
        var pull_url = self.data.rooms[index].PlayerUrl || '';
        listString += 'roomName=' + roomName + '&roomId=' + roomId + '&anthor=' + anthor + '&avatar=' + avatar + '&pull_url=' + encodeURIComponent(pull_url);
        wx.navigateTo({
            url: './player/player' + listString,
        })
    }
})